newTicketEditorProto = {
    id : 'newTicketEditorWindow'
    ,ticketForm : {}
    ,initComponent : function()
    {
        this.initContent();
        newTicketEditor.superclass.initComponent.call(this);
        this.ticketForm.nameAndTime.nameField.focus(true, 1000);
        sweatShop.globals.bindListeners(this.id, this.getListeners());
        sweatShop.globals.bindHandlers(this.id, this.getHandlers());
        this.ticketForm.assigneeCombo.getStore().load();
        this.ticketForm.productCombo.getStore().load();
    }
    , initContent : function ()
    {
        this.contentItems.buttons = this.buttonDefs;
        this.items = this.contentItems;
    }
    , getListeners : function()
    {
        return {
            colorPalette : {
                item : this.ticketForm.colorButton.menu.colorPalette
                , event: 'select'
                , fn : this.updateColor
                , scope : this
            }
            ,nameField : {
                item : this.ticketForm.nameAndTime.nameField
                , event: 'change'
                , fn : this.updateTitle
                , scope : this
            }
            ,ticketEditor : {
                item : this
                , event: 'afterrender'
                , fn : this.redrawBackground
            }
        };
    }
    , getHandlers : function()
    {
        return {
            assignToMeBtn : {
                item: this.ticketForm.assignToMeButton
                , fn : this.selectAssignee
                , scope : this
            }
            ,saveBtn : {
                item: this.ticketForm.saveButton
                , fn : this.saveAndCloseTicket
                , scope : this
            }
            ,saveNCopyBtn : {
                item: this.ticketForm.saveAndCopyButton
                , fn : this.saveAndCopyTicket
                , scope : this
            }
            ,saveNResetBtn : {
                item: this.ticketForm.saveAndResetButton
                , fn : this.saveAndResetTicket
                , scope : this
            }
            ,ntCancelBtn : {
                item: this.ticketForm.cancelButton
                , fn : this.close
                , scope : this
            }
        }
    }
    /**
     * Listeners bound to fields
     */
    , updateColor : function(palette, color)
    {
        this.ticketForm.ticketColorField.setValue(color);
        this.redrawBackground();
    }
    , updateTitle : function (field, newValue, oldValue)
    {
        this.setTitle(newValue);
    }
    , redrawBackground : function()
    {
        this.ticketForm.el.applyStyles({
            'background-color' : '#'+this.ticketForm.ticketColorField.getValue()
        });
    }
    /**
     * Handlerd bound to buttons
     */
    , selectAssignee : function()
    {
        this.ticketForm.assigneeCombo.setValue(sweatShop.session.userId);
    }
    , saveAndCloseTicket : function()
    {
        this.submitForm('Ticket/add', this.saveOkAndClose);
    }
    , saveAndCopyTicket : function()
    {
        this.submitForm('Ticket/add', this.saveOkAndCopy);
    }
    , saveAndResetTicket : function()
    {
        this.submitForm('Ticket/add', this.saveOkAndReset);
    }
    , submitForm : function (url, successFunc)
    {
        this.ticketForm.getForm().submit({
            url: url
            ,success : successFunc
            ,failure : globals.formSubmitFailed
            , scope : this
        });
    }
    , resetTicket : function()
    {
        this.ticketForm.getForm().reset();
        this.redrawBackground();
        this.setTitle('New ticket');
        this.ticketForm.nameAndTime.nameField.focus(true, 800);

    }
    , saveOkAndClose : function(form, action)
    {
        this.addTicketToBacklog(action.result.data);
        sweatShop.log(action.result.message, 1);
        this.hide();
        this.resetTicket();
    }
    , saveOkAndCopy : function(form, action)
    {
        this.addTicketToBacklog(action.result.data);
        sweatShop.log(action.result.message, 1);
        this.ticketForm.nameAndTime.nameField.setValue('');
        this.ticketForm.nameAndTime.nameField.focus(true, 1000);
        this.setTitle('New ticket');
    }
    , saveOkAndReset : function(form, action)
    {
        this.addTicketToBacklog(action.result.data);
        sweatShop.log(action.result.message, 1);
        this.resetTicket();
    }

    , addTicketToBacklog : function(ticketInfo)
    {
        if(ticketInfo.sprint == sweatShop.currentSprint
             && Ext.getCmp('sprintProductFilter').getValue() == sweatShop.currentSprint)
        {
            Ext.getCmp('backlogPanel').addTicket(ticketInfo);
        }
    }
   

    /**
     * Unrendered UI-components
     */
    , title : 'New ticket'
    , buttonDefs:[{
        text: 'Save \'n Close'
        , ref : '../saveButton'
        , formBind : true
    },{
        text : 'Save \'n Reset'
        ,ref : '../saveAndResetButton'
        , formBind : true
    },{
        text : 'Save \'n Copy'
        ,ref : '../saveAndCopyButton'
        , formBind : true
    },'-',{
        text : 'Cancel'
        ,ref : '../cancelButton'
    }]
};

newTicketEditor = Ext.extend(ticketEditorUi, newTicketEditorProto);
Ext.reg('newticketeditor', newTicketEditor);

existingTicketEditorProto = {

};
ticketEditor = Ext.extend(newTicketEditor, existingTicketEditorProto);
Ext.reg('ticketeditor', ticketEditor);